package com.hz.monitoring.module.hardware.socket.util;

public class BinHexOctUtil {
    //将字节数组转换为16进制字符串
    final static String HEX_STR = "0123456789ABCDEF";

    //********************************** start   Hex &  Ascii

    //hex 转 Ascii
    public static String hexToAscii(String hexStr) {
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < hexStr.length(); i += 2) {
            String str = hexStr.substring(i, i + 2);
            output.append((char) Integer.parseInt(str, 16));
        }
        return output.toString();
    }

    //ascii 转  Hex
    public static String asciiToHex(String asciiStr) {
        char[] chars = asciiStr.toCharArray();
        StringBuilder hex = new StringBuilder();
        for (char ch : chars) {
            hex.append(Integer.toHexString(ch));
        }
        return hex.toString();
    }
    //********************************** end  Hex &  Ascii 

    //********************************** start   Hex &  binary   16进制 与  二进制 

    public static String binaryToHexString(byte[] bytes) {
        StringBuffer sb = new StringBuffer(bytes.length);
        for (byte b : bytes) {
            String hex = String.valueOf(HEX_STR.charAt((b & 0xF0) >> 4));
            hex += String.valueOf(HEX_STR.charAt(b & 0x0F));
            sb.append(hex);
        }
        return sb.toString();
    }

    // Hex 转  binary
    public static byte[] hexStringToBytes(String hexString) {
        int l = hexString.length() / 2;
        byte[] ret = new byte[l];
        for (int i = 0; i < l; i++) {
            ret[i] = Integer.valueOf(hexString.substring(i * 2, i * 2 + 2), 16).byteValue();
        }
        return ret;
    }
    //********************************** end   Hex &  binary   16进制 与  二进制 


    //********************************** start   Hex &  String   16进制 与  10进制

    //16进制字符串换为10进制字符串
    public static String hexStringToString(String hexString) {
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < hexString.length(); i += 2) {
            String str = hexString.substring(i, i + 2);
            output.append(Integer.parseInt(str, 16));
        }
        return output.toString();
    }


    public static String stringToHexString(String str) {
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            String s4 = Integer.toHexString(str.charAt(i));
            output.append(s4);
        }
        return output.toString();
    }

    /**
     * 数字转Hex
     *
     * @param i
     * @param length 输出的长度
     * @return
     */
    public static String intToHexString(int i, int length) {
        String hex = Integer.toHexString(i).toUpperCase();
        while (hex.length() < length) {
            hex = "0" + hex;
        }

        return hex;
    }
    //********************************** end     Hex &  String   16进制 与  10进制

    /**
     * CRC校验码
     *
     * @param hexString 十六进制字符串
     * @param format    是否需要高低位互换，佳岚的需要高低位互换
     * @return CRC校验码
     */
    public static String makeCRC(String hexString, boolean format) {
        byte[] bytes = hexStringToBytes(hexString);
        int crc = 0x0000ffff;
        for (int i = 0; i < bytes.length; i++) {
            crc ^= ((int) bytes[i] & 0x000000ff);
            for (int j = 0; j < 8; j++) {
                if ((crc & 0x00000001) != 0) {
                    crc >>= 1;
                    crc ^= 0x0000a001;
                } else {
                    crc >>= 1;
                }
            }
        }
        // 高低位互换，输出符合相关工具对Modbus CRC16的运算
        if (format) {
            crc = ((crc & 0xff00) >> 8) | ((crc & 0x00ff) << 8);
        }
        return String.format("%04X", crc);
    }


}
